<?php

	namespace Phptricks\system\library\session;

	use Phptricks\system\core\library\library;
	use Phptricks\system\library\PhptricksExceptions\PhptricksExceptions as Exc;

	class session extends \SessionHandler implements library{

		static $_instance = null;

		public function __construct(){
			session_start();
		}

		public static function getinstance(){
			if (self::$_instance === null) {
				self::$_instance = new \Phptricks\system\library\session\session();
			}
			return self::$_instance;
		}

		public static function is_set($key){
			if (empty($key) || !isset($key)) {
				throw new Exc('$key is not set');
			}
			if (isset($_SESSION[$key])) {
				return true;
			}
			return false;
		}

		public static function set($key, $val = ''){
			if (empty($key) && !isset($key)) {
				throw new Exc('$key is not set');
			}
			if (\is_array($key)) {
				self::_set_session_by_array($key);
			} else {
				$_SESSION[$key] = $val;
			}
		}

		private static function _set_session_by_array($array_session = array()){
			if (!isset($array_session)) {
				throw new Exc('$array_session is not set');
			}
			if (\is_array($array_session) && count($array_session) > 0) {
				foreach ($array_session as $key => $val) {
					if (!empty($key)) {
						self::set($key, $val);
					}
				}
				unset($array_session);
			}
		}

		public static function get($key = ''){
			if (empty($key) || !isset($key)) {
				return $_SESSION;
			}
			if (\is_array($key)) {
				return self::_get_session_by_array($key);
			}
			if (self::is_set($key) == true) {
				return $_SESSION[$key];
			}
		}

		private static function _get_session_by_array($array_session = array()){
			$array_return = array();
			if (is_array($array_session) && count($array_session) > 0) {
				foreach ($array_session as $key) {
					if (!empty($key)) {
						$array_return[$key] = self::get($key);
					}
				}
			}
			return $array_return;
		}

		public static function delete($key){
			if (\is_array($key) && \count($key) > 0) {
				self::_delete_by_array($key);
			} else {
				unset($_SESSION[$key]);
			}
			return true;
		}

		private static function _delete_by_array($array_session = array()){
			if (is_array($array_session) && count($array_session) > 0) {
				foreach ($array_session as $key) {
					if (!empty($key)) {
						self::delete($key);
					}
				}
			}
		}

		public static function empty_session($key){
			$this->set_session($key, '');
		}

		public static function clear_session(){
			session_destroy();
		}

	}

?>